<template>

  <div class="mod-config">
    <el-form :inline="true" label-width="160px" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.cdkey" style="width:280px;font-size:10px" placeholder="cdk卡密" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.remark" placeholder="备注" style="width:150px;font-size:10px" clearable></el-input>
      </el-form-item>
      <el-form-item prop="status">
        <el-select v-model="dataForm.status" style="width:110px;font-size:12px" filterable placeholder="卡密状态">
          <el-option label="卡密状态" value="" />
          <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="cdkType">
        <el-select v-model="dataForm.cdkType" style="width:125px;font-size:12px" filterable placeholder="卡密类型">
          <el-option label="卡密类型" value="" />
          <el-option v-for="item in cdkTypeList" :key="item.code" :label="item.value" :value="item.code" :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="dateTypeList">
        <el-select v-model="dataForm.cdkDateType" style="width:110px;font-size:12px" filterable placeholder="日期类型">
          <el-option label="日期类型" value="" />
          <el-option v-for="item in dateTypeList" :key="item.code" :label="item.value" :value="item.code" :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="agentList">
        <el-select v-model="dataForm.agentId" style="width:110px;font-size:12px" filterable placeholder="代理">
          <el-option label="代理" value="" />
          <el-option v-for="item in agentList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="activatedStatus">
        <el-select v-model="dataForm.activatedStatus" style="width:110px;font-size:12px" filterable placeholder="激活状态">
          <el-option label="激活状态" value="" />
          <el-option v-for="item in activatedStatusList" :key="item.value" :label="item.label" :value="item.value"
            :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="agenticState">
        <el-select v-model="dataForm.agenticState" style="width:110px;font-size:12px" filterable placeholder="推送添加">
          <el-option label="推送添加" value="" />
          <el-option v-for="item in agenticStateList" :key="item.value" :label="item.label" :value="item.value"
            :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="dataForm.startDate" type="datetime" style="width:185px;font-size:12px" placeholder="选择开始时间"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="dataForm.endDate" type="datetime" style="width:185px;font-size:12px" placeholder="选择截至时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-form>
      <el-form-item>
        <!-- <el-button v-if="isAuth('cards:amgcdkcards:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <el-button v-if="isAuth('cards:amgcdkcards:save')" type="primary" @click="batchAddHandle()">新增卡密</el-button>
        <el-button v-if="isAuth('cards:amgcdkcards:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">删除</el-button>
        <el-button v-if="isAuth('cards:amgcdkcards:update')" type="danger" @click="batchWholesaleHandle()" :disabled="dataListSelections.length <= 0">修改状态</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column type="index" header-align="center" align="center" label="序号" width="50" />
      <el-table-column prop="cdkey" header-align="center" align="center" label="卡密" width="300">
      </el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="卡密状态" width="80" :formatter="formatStatus">
      </el-table-column>
      <el-table-column prop="cdkTypeName" header-align="center" align="center" label="cdk类型" width="100">
      </el-table-column>
      <el-table-column prop="cdkDateTypeName" header-align="center" align="center" label="日期类型" width="80">
      </el-table-column>
      <el-table-column prop="activatedStatus" header-align="center" align="center" label="激活状态" width="80" :formatter="formatActivatedStatus">
      </el-table-column>
      <el-table-column prop="employDate" header-align="center" align="center" label="使用时间" width="100">
      </el-table-column>
      <el-table-column prop="ineffectiveDate" header-align="center" align="center" label="失效时间" width="100">
      </el-table-column>
      <el-table-column prop="agentName" header-align="center" align="center" label="代理">
      </el-table-column>
      <el-table-column prop="agenticState" header-align="center" align="center" label="推送添加" width="80" :formatter="formatAgenticState">
      </el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注">
      </el-table-column>
      <el-table-column prop="createdBy" header-align="center" align="center" label="创建人" width="90">
      </el-table-column>
      <el-table-column prop="createdDate" header-align="center" align="center" label="创建时间" width="100">
      </el-table-column>
      <!-- <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>


    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100,200,300,500]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <batch-add v-if="batchAddVisible" ref="batchAdd" @refreshDataList="getDataList"></batch-add>
    <batch-wholesale v-if="batchWholesaleVisible" ref="batchWholesale" @refreshDataList="getDataList"></batch-wholesale>
  </div>
</template>
<script>
  import AddOrUpdate from './amgcdkcards-add-or-update'
  import BatchAdd from './amgcdkcards-batch-add'
  import BatchWholesale from './amgcdkcards-batch-wholesale'
  export default {
    data() {
      return {
        dataForm: {
          cdkey: '',
          remark: '',
          status: '',
          cdkType: '',
          cdkDateType: '',
          activatedStatus: '',
          agenticState: '',
          startDate: '',
          endDate: '',
          agentId: ''

        },
        dataList: [],
        cdkTypeList: [],
        dateTypeList: [],
        agentList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        batchAddVisible: false,
        batchWholesaleVisible: false,
        statusList: [{
          value: 1,
          label: '新生成'
        }, {
          value: 2,
          label: '已卖出',

        }, {
          value: 3,
          label: '已使用'
        }, {
          value: 4,
          label: '已作废'
        }, {
          value: 5,
          label: '已失效'
        }],
        agenticStateList: [{
          value: 1,
          label: '推送',
        }, {
          value: 2,
          label: '不推送',

        }],
        activatedStatusList: [{
          value: 1,
          label: '未激活',
        }, {
          value: 2,
          label: '已激活',

        }]

      }
    },
    components: {
      AddOrUpdate,
      BatchAdd,
      BatchWholesale
    },
    activated() {
      this.getDataList()
      this.getDictonariesSelectList()
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/cards/amgcdkcards/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'cdkey': this.dataForm.cdkey,
            'remark': this.dataForm.remark,
            'status': this.dataForm.status,
            'cdkType': this.dataForm.cdkType,
            'cdkDateType': this.dataForm.cdkDateType,
            'activatedStatus': this.dataForm.activatedStatus,
            'agenticState': this.dataForm.agenticState,
            'startDate': this.dataForm.startDate,
            'endDate': this.dataForm.endDate,
            'agentId': this.dataForm.agentId
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      getDictonariesSelectList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/cards/amgcdkcards/getDictonariesSelectList'),
          method: 'get'
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.cdkTypeList = data.cdkTypeList
            this.dateTypeList = data.dateTypeList
            this.agentList = data.agentList
          } else {
            this.cdkTypeList = []
            this.dateTypeList = []
            this.agentList = []
          }
          this.dataListLoading = false
        })
      },
      formatStatus: function (row, column) {
        var str = ''
        switch (row.status) {
          case 1:
            str = '新生成'
            break
          case 2:
            str = '已卖出'
            break
          case 3:
            str = '已使用'
            break
          case 4:
            str = '已作废'
            break
          case 5:
            str = '已失效'
            break
          default:
            str = ''
            break

        }
        return str
      },
      formatActivatedStatus: function (row, column) {
        var str = ''
        switch (row.activatedStatus) {
          case 1:
            str = '未激活'
            break
          case 2:
            str = '已激活'
            break
        }
        return str
      },
      formatAgenticState: function (row, column) {
        var str = ''
        switch (row.agenticState) {
          case 0:
            str = '不推送'
            break
          case 1:
            str = '推送'
            break
          case 2:
            str = '不推送'
            break
        }
        return str
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      //批量新增
      batchAddHandle() {
        this.batchAddVisible = true
        this.$nextTick(() => {
          this.$refs.batchAdd.init(this.cdkTypeList, this.dateTypeList)
        })
      },
      //批量批发
      batchWholesaleHandle() {
        var ids = this.dataListSelections.map(item => {
          return item.id
        })
        this.batchWholesaleVisible = true
        this.$nextTick(() => {
          this.$refs.batchWholesale.init(ids, this.agentList)
        })
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/cards/amgcdkcards/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
